package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.HtmlCreator;

/**
 * Servlet implementation class ParamsServlet
 */
//@WebServlet("/ParamsServlet")
public class ParamsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ParamsServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//输出Header头部信息
		String webContent = "<h1 align=\"center\">Http 输出Header</h1>\n"
				+ "<table width=\"100%\" border=\"1\" align=\"center\">\n"
				+ "<tr bgcolor=\"#949494\">"
				+"<th>Header 名称</th><th>Header 值</th>\n"+
	            "</tr>\n";
		Enumeration<String> headers = request.getHeaderNames();
		StringBuffer webTable = new StringBuffer();
		while (headers.hasMoreElements()) {
			String headerName = (String) headers.nextElement();
			String headerValue =  request.getHeader(headerName);
			webTable.append("<tr><td>"+headerName+"</td>\n");
			webTable.append("<td>"+headerValue+"</td></tr>");
		}
		
		PrintWriter writer = response.getWriter();
		writer.print(HtmlCreator.createWebContent("Servlet获取数据", webContent+webTable.toString()));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
